<?php

namespace App\Tools;


use Illuminate\Support\Facades\Redis;

class Wechat
{

    public static function curlPost($url, $data)
    {
        $curl = curl_init();
        curl_setopt_array($curl,
            array(CURLOPT_URL => $url,
                CURLOPT_RETURNTRANSFER => true,
                CURLOPT_ENCODING => '',
                CURLOPT_MAXREDIRS => 10,
                CURLOPT_TIMEOUT => 0,
                CURLOPT_FOLLOWLOCATION => true,
                CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
                CURLOPT_CUSTOMREQUEST => 'POST',
                CURLOPT_POSTFIELDS => json_encode($data),
                CURLOPT_HTTPHEADER => array('Content-Type: application/json')));
        $response = curl_exec($curl);
        curl_close($curl);
        return $response;
    }
    public static function getOpenId($code, $token)
    {
        $uri = 'https://api.weixin.qq.com/wxa/getpluginopenpid?access_token=' . $token;
        $data = [
            'code' => $code
        ];
        $response = self::curlPost($uri,$data);
        return $response;
    }

    /**获取手机号
     * @param string $token
     * @param string $openId
     * @param string $code
     * @return mixed
     * @throws \Exception
     */
    public static function getPhoneNumber(string $token, string $code)
    {
        $uri = 'https://api.weixin.qq.com/wxa/business/getuserphonenumber?access_token=' . $token;
        $data = [
            'code' => $code
        ];
        $rt = self::curlPost($uri,$data);
        $rt = json_decode($rt, true);
        return $rt;
    }

    //获取小程序access_token
    public static function getAccessToken()
    {

        $appid = 'wx5fb830b234320db1';
        $secret = 'df3727e7cae40c0c6d2c9b418edc69f1';
        if(Redis::exists('access_token_applets')){
            return Redis::get('access_token_applets');
        }
        $url = 'https://api.weixin.qq.com/cgi-bin/stable_token';
        $data = [
            'grant_type' => 'client_credential',
            'appid' => $appid,
            'secret' => $secret
        ];
        $rt = self::httpPostRequest($url, $data);
        $rt = json_decode($rt, true);
        if (!isset($rt['access_token'])) {
            return '';
        }
        Redis::setex('access_token_applets', 7200, $rt['access_token']);
        return $rt['access_token'];

    }



    /**微信curl
     * @param $url
     * @param $parameters
     * @param $common_parameters
     * @return bool|string
     * @throws \Exception
     */
    public static function httpPostRequest($url = '', $parameters = [], $common_parameters = [])
    {


        $parameters = json_encode($parameters);
        $request_url = $url . '?' . http_build_query($common_parameters);
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $request_url);
        curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 60);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($curl, CURLOPT_POST, 1);
        curl_setopt($curl, CURLOPT_POSTFIELDS, $parameters);
        curl_setopt($curl, CURLOPT_HTTPHEADER, array(
            "Content-Type:application/json"
        ));
        $response = curl_exec($curl);
        if (curl_error($curl)) {
            $error_msg = curl_error($curl);
            $error_no = curl_errno($curl);
            curl_close($curl);
            throw new \Exception($error_msg, $error_no);
        }
        curl_close($curl);
        return $response;

    }

    /**
     * curl post json 请求
     * */
    public static function postCurl($url, $data, $json = false)
    {
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
        if (!empty($data)) {
            if ($json && is_array($data)) {
                $data = json_encode($data);
            }
            curl_setopt($curl, CURLOPT_POST, 1);
            curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
            if ($json) { //发送JSON数据
                curl_setopt($curl, CURLOPT_HEADER, 0);
                curl_setopt($curl, CURLOPT_HTTPHEADER,
                    array('Content-Type: application/json; charset=utf-8', 'Content-Length:' . strlen($data)));
            }
        }
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        $res = curl_exec($curl);
        $errorno = curl_errno($curl);
        if ($errorno) {
            return array('errorno' => false, 'errmsg' => $errorno);
        }
        curl_close($curl);
        return $res;
    }


}
